//
//  XTVisitorView.swift
//  HrenryWeibo
//
//  Created by 熊涛 on 16/5/11.
//  Copyright © 2016年 熊涛. All rights reserved.
//

import UIKit

//1.定义协议
@objc protocol XTVisitorViewDelegate : NSObjectProtocol {
    //2.定义方法
    
    //optional 前面加optional是可选的 可实现,可不实现的
    //没加optional 是一定要实现的
    
    /**
    点击注册按钮
    */
    func registerBtnClick()
    /**
     点击登录按钮
     */
    func loginBtnClick()

}


class XTVisitorView: UIView {

    //MARK: - 代理
    //3.设置代理
    var delegate : XTVisitorViewDelegate?
    
    //MARK: - 回调属性
//    var registerCallback : (() -> ())?
//    var loginCallback : (() -> ())?
    
    //MARK: - lifecycle
    
    override init(frame: CGRect) {
        //在super.init之前给属性赋值
        //调用对象方法不能放到super.init之前,在super.init之前,这个对象还没有初始化好(创建),对象没创建好是不能调用对象方法的
        super.init(frame: frame)
        
        
        /**
            添加UI控件
        */
        setUpUI()
        
    }

    
    //这个构造函数是通过sb/xib里面创建才会走这个方法
    //UIKit的里面的view 即可以通过代码来创建,也可以通过sb/xib来创建
    /**
      required  表示必须实现的构造函数有required,不需要加override
    */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //MARK: - 添加UI
    func setUpUI() {
    
//        self.backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)

        
        //添加子控件
        self.addSubview(iconView)
        self.addSubview(coverView)
        self.addSubview(homeView)
        self.addSubview(textField)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        textField.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        
       //设置iconview
        self.addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //设置遮盖视图
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30))
        
        //设置homeview
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //设置textFiled
        self.addConstraint(NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //当参照的控件为nil时,属性NSLayoutAttribute.NotAnAttribute
        self.addConstraint(NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 0, constant: 240))
        self.addConstraint(NSLayoutConstraint(item: textField, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //设置registerBtn
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: textField, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //设置loginBtn
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: textField, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textField, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 30))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))

    }
    
    
    //MARK: - 设置访问视图的子控件内容
    func setVisitorsubViewsInfo(imageNmae : String,textfield : String) {
    
        //将转轮替换掉
        self.iconView.image = UIImage(named: imageNmae)
        
        //隐藏掉小房子
        self.homeView.hidden = true
        
        //textFieldl内容
        self.textField.text = textfield
        
    }
    
    //MARK: - 转轮旋转
    func startRotate() {
        
        //创建核心动画--基本动画类型  并设置keypath
       let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        //循环的次数
        animation.repeatCount = MAXFLOAT
        
        //核心动画完成的时候不要移除
        animation.removedOnCompletion = false
        
        //将核心动画添加到iconview的图层上
        iconView.layer.addAnimation(animation, forKey: nil)
        
    
    }
    
    
    //MARK: - 按钮点击事件
    //按钮点击事件是由系统来调用的,当把按钮点击事件加上private,系统就找不到这个方法了
    //@objc : 让系统可以找到我们的方法
   @objc private func registerBtnClickAction() {
    
//        registerCallback?()
        //4.调用代理方法
        delegate?.registerBtnClick()
    }
    
   @objc private func loginBtnClickAction() {
    
//        loginCallback?()
        //4.调用代理方法
        delegate?.loginBtnClick()
    }
    
    
    
    
    //MARK: - 懒加载
    //private 私有方法
    
    /// 转轮
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
   /// 小房子
    private lazy var homeView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    /// lavel
    private lazy var textField : UILabel = {
        let label = UILabel()
        //设置
        label.text = "关注一些人,看看有什么惊喜!"
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.numberOfLines = 0
        //放label自适应文字大小
        label.sizeToFit()
        return label
    }()
    
    /// 注册按钮
    private lazy var registerBtn : UIButton = {
        let register = UIButton(type: UIButtonType.Custom)
        
        //设置
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        register.setTitle("注册", forState: UIControlState.Normal)
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        register.addTarget(self, action: "registerBtnClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
        
    }()
    
    /// 登录按钮
    private lazy var loginBtn : UIButton = {
        let login = UIButton(type: UIButtonType.Custom)
        
        //设置
        login .setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.setTitle("登录", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        login.addTarget(self, action: "loginBtnClickAction", forControlEvents: UIControlEvents.TouchUpInside)
        
        return login
        
    }()
    
    /// 遮盖视图
    lazy var coverView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}
